#ifndef AUTHORIZATIONTIMING_H
#define AUTHORIZATIONTIMING_H

#include <QObject>
#include <QTimer>
#include <QDateTime>
#include <QFile>
#include <QCryptographicHash>
#include <QSettings>
#include <QThread>
#include <QFuture>

class AuthorizationTiming : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int remainingTime READ remainingTime NOTIFY remainingTimeChange) //剩余多少秒

public:
    AuthorizationTiming(QObject *parent = nullptr);

    Q_INVOKABLE void openAuthorizetion();    //加载完成打开
    Q_INVOKABLE int authorizationuser(const QString &plainText); //flase ture.
    Q_INVOKABLE int authorizationuserBytime(const QString &plainText); //flase ture.
    Q_INVOKABLE void setTotalTime(int seconds);      // 设置总的倒计时  -520

    void resetTotalTime(int seconds);    // 重置总的倒计时
    void startCountdown();               // 开始倒计时
    void stopCountdown();                // 停止倒计时
    void loadTimeFromFile();             // 从文件中加载倒计时
    void saveTimeToFile();               // 保存倒计时到文件
    int remainingTime() const {return m_remainingTime;};

signals:
    void authorizationExpires();   //到期
    void remainingTimeChange();
private slots:
    void updateCountdown();
private:
    QTimer expirationdateTimer;
    QString timing_dataPath = "master/config/data.dat";
    QByteArray secret_key = "asznwstc";
    QString  secret = "asznwstc";
    int m_totalTime;
    const int m_maxtotail = 40121012;        // 总的倒计时（秒）
    int m_remainingTime = -1;                 // 剩余的时间（秒） /24121012
    QTimer *m_timer;                     // 用于倒计时的定时器
    QByteArray encryptTime(int time);       // 加密时间
    int decryptTime(const QByteArray &data); // 解密时间.
    bool m_running;
    QFuture<void> m_future; // 用于管理异步任务
    int secondsElapsed = 0;
};

#endif // AUTHORIZATIONTIMING_H
